//
//  UserAccount.swift
//  sinaweibowj
//
//  Created by Macintosh on 15/12/17.
//  Copyright © 2015年 mactoinsh. All rights reserved.
//

import UIKit
//自定义对象归档操作
class UserAccount: NSObject,NSCoding {
    //用于调用access_token，接口获取授权后的access token。
    var access_token: String?
    //access_token的生命周期，单位是秒数。
    var expires_in: NSTimeInterval = 0{
        didSet {
        expires_date = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    //token的过期日期  开发者账号的token的有效期 是 5年  普通用户的账号有效期 是 3天
    var expires_date: NSDate?
    //	当前授权用户的UID。
    var uid: String?
    //友好显示名称
    var name: String?
    //用户头像地址（大图），180×180像素
    var avatar_large: String?
    
    //KVC给对象赋值
    init(dict :[String:AnyObject]){
        
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    //只读属性，也叫计算性属性
    override var description:String{
        
        let keys = ["access_token","expires_in","uid","name","avatar_large","expires_date"]
        
        return dictionaryWithValuesForKeys(keys).description
    }
    
    
    //保存自定义对象
    func saveAccount (){
        
        let file  = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        
        //保存自定义对象
        NSKeyedArchiver.archiveRootObject(self, toFile: file)
    }
    
    //class func 表示的是类方法
    class func loadAccount() -> UserAccount? {
        
        //从磁盘解档数据
        let file = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("account.plist")
        
        if let account = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? UserAccount{
            //print(account)
            //对token有效期做判断
            if account.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending {
            
                return account
            }
        }
        
        return nil
    }
    
    //解档操作 二进制数据 -》自定义对象  反序列化类似
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
    }
    
    //归档  自定义对象- 》二进制数据  序列化
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
    }
    
}
